package com.google.gwt.sample.dynatablerf.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;

public class GrifoSearchBox extends Composite implements
		HasSelectionHandlers<Suggestion>, HasText, HasMenuItens {

	private SuggestBox suggestBox;
	private MenuBar optionsMenu;

	public GrifoSearchBox(SuggestBox suggestBox, MenuBar optionsMenu) {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);

		this.suggestBox = suggestBox;
		horizontalPanel.add(suggestBox);

		this.optionsMenu = optionsMenu;
		horizontalPanel.add(optionsMenu);

		this.suggestBox.getValueBox().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				setText("");

			}
		});

	}

	public MenuBar getOptionsMenu() {
		return optionsMenu;
	}

	public SuggestBox getSuggestBox() {
		return suggestBox;
	}

	@Override
	public HandlerRegistration addSelectionHandler(
			SelectionHandler<Suggestion> handler) {
		return suggestBox.addSelectionHandler(handler);
	}

	@Override
	public String getText() {
		return suggestBox.getText();
	}

	@Override
	public void setText(String text) {
		suggestBox.setText(text);

	}

	@Override
	public MenuBar getMenuBar() {
		return optionsMenu;
	}

}
